

jQuery(document).ready(function() {
	
	initTable();
	
	App.setPage("opinion");  //Set current page
	App.init(); //Initialise plugins and elements
	
	bindDict("roletype", "DM_ROLETYPE", null);

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	
	//新增字典
	$('#addRole').click(function(){
    	setModalSize();
    	
    	$('#ifModal').attr("src", "add.html");
    	
		$('#modalTile').html("常用意见新增");
		$("#modal").modal('show');
	});
	
	//批量删除
	$('#batchDelete').click(function(){
		
		var data = $('#table').bootstrapTable('getSelections');
		if(data.length <= 0){
			window.wxc.xcConfirm('请勾选要<strong>批量删除</strong>的常用意见数据', window.wxc.xcConfirm.typeEnum.info);
			return;
		}
		var commonopinionids = "";
		for(var i = 0; i < data.length; i++){
			if(data[i].roletype != '02')
				commonopinionids += data[i].commonopinionid + ",";
		}
		
		commonopinionids = commonopinionids.substring(0, commonopinionids.length - 1);
		window.wxc.xcConfirm('是否<strong>批量删除</strong>常用意见数据?<br /><span style="color:red;">['+ commonopinionids +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'sys/batchdeletecommonopinion.do',
					data: {"commonopinionids": commonopinionids},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){        							        							
	        	            window.wxc.xcConfirm('删除常用意见['+ data.datas[0].commonopinionids +']成功!', window.wxc.xcConfirm.typeEnum.info);
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
	});
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
	//清空表单数据事件
	$("#modalDict").on("hidden.bs.modal", function() {
		$('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').remove();	
		if($('#' + tableID).find('tbody').children('tr').length == 0){
			var trStr = '<tr class="no-records-found">	'+
		    '   <td colspan="7" style="font-size:14px;">			'+
			'		单充底部“新增行”增加字典！		'+
			'	</td>						'+
			'</tr>';
			
			$('#' + tableID).find('tbody').append(trStr);
		}
		
		$(this).find('[type="text"]').val('');
		$(this).find('[type="checkbox"]').prop("checked",true);         	
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'sys/opinionlist.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	opinion : $('#opinion').val()
                };
        },
	    columns: [{
            field : 'checked',
            checkbox : true

        }, {
	        field: 'opinion',
	        title: '意见',
	        formatter:function(value,row,index){
	        	return  value;
	        }
	    }, {
	        field: 'count',
	        title: '优先级'
	    },  {
            field: 'operate',
            title: '操作',
            align: 'center',
            width: '300px',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
}

function operateFormatter(value, row, index) {
	if(row.roletype != '02'){
	    return [
	        '<button type="button" class="Edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
	        '<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>'
	    ].join('');
	}
}

window.operateEvents = {
	'click .Edit': function (e, value, row, index) {
    	var commonopinionid = row.commonopinionid;
    	
    	setModalSize();
    	
    	$('#ifModal').attr("src", "edit.html?commonopinionid=" + commonopinionid);
    	
		$('#modalTile').html("常用意见修改");
		$("#modal").modal('show');
    },
    'click .Delete': function (e, value, row, index) {

    	var commonopinionid = row.commonopinionid;
		window.wxc.xcConfirm('是否<strong>删除</strong>常用意见数据?<br /><span style="color:red;">['+ commonopinionid +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'sys/deletecommonopinion.do',
					data: {"commonopinionid": commonopinionid},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
    }
 };


function showDetail(rolecode){
	setModalSize();
	
	$('#ifModal').attr("src", "detail.html?rolecode=" + rolecode);
	
	$('#modalTile').html("常用意见详细");
	$("#modal").modal('show');
}

